home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / scheme / scm / test.scm < prev    next >
Text File  |  1994-10-23  |  27KB  |  961 lines

  1. ;;;; `test.scm' Test correctness of scheme implementations.
  2. ;;; Copyright (C) 1991, 1992, 1993, 1994 Aubrey Jaffer.
  3.  
  4. ;;; This includes examples from
  5. ;;; William Clinger and Jonathan Rees, editors.
  6. ;;; Revised^4 Report on the Algorithmic Language Scheme
  7. ;;; and the IEEE specification.
  8.  
  9. ;;; The input tests read this file expecting it to be named "test.scm".
  10. ;;; Files `tmp1', `tmp2' and `tmp3' will be created in the course of running
  11. ;;; these tests.  You may need to delete them in order to run
  12. ;;; "test.scm" more than once.
  13.  
  14. ;;;   There are three optional tests:
  15. ;;; (TEST-CONT) tests multiple returns from call-with-current-continuation
  16. ;;; 
  17. ;;; (TEST-SC4) tests procedures required by R4RS but not by IEEE
  18. ;;; 
  19. ;;; (TEST-DELAY) tests DELAY and FORCE, which are not required by
  20. ;;;   either standard.
  21.  
  22. ;;; If you are testing a R3RS version which does not have `list?' do:
  23. ;;; (define list? #f)
  24.  
  25. ;;; send corrections or additions to jaffer@ai.mit.edu or
  26. ;;; Aubrey Jaffer, 84 Pleasant St., Wakefield MA 01880, USA
  27.  
  28. (define cur-section '())(define errs '())
  29. (define SECTION (lambda args
  30.           (display "SECTION") (write args) (newline)
  31.           (set! cur-section args) #t))
  32. (define record-error (lambda (e) (set! errs (cons (list cur-section e) errs))))
  33.  
  34. (define test
  35.   (lambda (expect fun . args)
  36.     (write (cons fun args))
  37.     (display "  ==> ")
  38.     ((lambda (res)
  39.       (write res)
  40.       (newline)
  41.       (cond ((not (equal? expect res))
  42.          (record-error (list res expect (cons fun args)))
  43.          (display " BUT EXPECTED ")
  44.          (write expect)
  45.          (newline)
  46.          #f)
  47.         (else #t)))
  48.      (if (procedure? fun) (apply fun args) (car args)))))
  49. (define (report-errs)
  50.   (newline)
  51.   (if (null? errs) (display "Passed all tests")
  52.       (begin
  53.     (display "errors were:")
  54.     (newline)
  55.     (display "(SECTION (got expected (call)))")
  56.     (newline)
  57.     (for-each (lambda (l) (write l) (newline))
  58.           errs)))
  59.   (newline))
  60.  
  61. (SECTION 2 1);; test that all symbol characters are supported.
  62. '(+ - ... !.. $.+ %.- &.! *.: /:. :+. <-. =. >. ?. ~. _. ^.)
  63.  
  64. (SECTION 3 4)
  65. (define disjoint-type-functions
  66.   (list boolean? char? null? number? pair? procedure? string? symbol? vector?))
  67. (define type-examples
  68.   (list
  69.    #t #f #\a '() 9739 '(test) record-error "test" "" 'test '#() '#(a b c) ))
  70. (define i 1)
  71. (for-each (lambda (x) (display (make-string i #\ ))
  72.           (set! i (+ 3 i))
  73.           (write x)
  74.           (newline))
  75.       disjoint-type-functions)
  76. (define type-matrix
  77.   (map (lambda (x)
  78.      (let ((t (map (lambda (f) (f x)) disjoint-type-functions)))
  79.        (write t)
  80.        (write x)
  81.        (newline)
  82.        t))
  83.        type-examples))
  84. (SECTION 4 1 2)
  85. (test '(quote a) 'quote (quote 'a))
  86. (test '(quote a) 'quote ''a)
  87. (SECTION 4 1 3)
  88. (test 12 (if #f + *) 3 4)
  89. (SECTION 4 1 4)
  90. (test 8 (lambda (x) (+ x x)) 4)
  91. (define reverse-subtract
  92.   (lambda (x y) (- y x)))
  93. (test 3 reverse-subtract 7 10)
  94. (define add4
  95.   (let ((x 4))
  96.     (lambda (y) (+ x y))))
  97. (test 10 add4 6)
  98. (test '(3 4 5 6) (lambda x x) 3 4 5 6)
  99. (test '(5 6) (lambda (x y . z) z) 3 4 5 6)
  100. (SECTION 4 1 5)
  101. (test 'yes 'if (if (> 3 2) 'yes 'no))
  102. (test 'no 'if (if (> 2 3) 'yes 'no))
  103. (test '1 'if (if (> 3 2) (- 3 2) (+ 3 2)))
  104. (SECTION 4 1 6)
  105. (define x 2)
  106. (test 3 'define (+ x 1))
  107. (set! x 4)
  108. (test 5 'set! (+ x 1))
  109. (SECTION 4 2 1)
  110. (test 'greater 'cond (cond ((> 3 2) 'greater)
  111.                ((< 3 2) 'less)))
  112. (test 'equal 'cond (cond ((> 3 3) 'greater)
  113.              ((< 3 3) 'less)
  114.              (else 'equal)))
  115. (test 2 'cond (cond ((assv 'b '((a 1) (b 2))) => cadr)
  116.              (else #f)))
  117. (test 'composite 'case (case (* 2 3)
  118.              ((2 3 5 7) 'prime)
  119.              ((1 4 6 8 9) 'composite)))
  120. (test 'consonant 'case (case (car '(c d))
  121.              ((a e i o u) 'vowel)
  122.              ((w y) 'semivowel)
  123.              (else 'consonant)))
  124. (test #t 'and (and (= 2 2) (> 2 1)))
  125. (test #f 'and (and (= 2 2) (< 2 1)))
  126. (test '(f g) 'and (and 1 2 'c '(f g)))
  127. (test #t 'and (and))
  128. (test #t 'or (or (= 2 2) (> 2 1)))
  129. (test #t 'or (or (= 2 2) (< 2 1)))
  130. (test #f 'or (or #f #f #f))
  131. (test #f 'or (or))
  132. (test '(b c) 'or (or (memq 'b '(a b c)) (+ 3 0)))
  133. (SECTION 4 2 2)
  134. (test 6 'let (let ((x 2) (y 3)) (* x y)))
  135. (test 35 'let (let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x))))
  136. (test 70 'let* (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))))
  137. (test #t 'letrec (letrec ((even?
  138.                (lambda (n) (if (zero? n) #t (odd? (- n 1)))))
  139.               (odd?
  140.                (lambda (n) (if (zero? n) #f (even? (- n 1))))))
  141.            (even? 88)))
  142. (define x 34)
  143. (test 5 'let (let ((x 3)) (define x 5) x))
  144. (test 34 'let x)
  145. (test 6 'let (let () (define x 6) x))
  146. (test 34 'let x)
  147. (test 7 'let* (let* ((x 3)) (define x 7) x))
  148. (test 34 'let* x)
  149. (test 8 'let* (let* () (define x 8) x))
  150. (test 34 'let* x)
  151. (test 9 'letrec (letrec () (define x 9) x))
  152. (test 34 'letrec x)
  153. (test 10 'letrec (letrec ((x 3)) (define x 10) x))
  154. (test 34 'letrec x)
  155. (SECTION 4 2 3)
  156. (define x 0)
  157. (test 6 'begin (begin (set! x 5) (+ x 1)))
  158. (SECTION 4 2 4)
  159. (test '#(0 1 2 3 4) 'do (do ((vec (make-vector 5))
  160.                 (i 0 (+ i 1)))
  161.                ((= i 5) vec)
  162.              (vector-set! vec i i)))
  163. (test 25 'do (let ((x '(1 3 5 7 9)))
  164.            (do ((x x (cdr x))
  165.             (sum 0 (+ sum (car x))))
  166.            ((null? x) sum))))
  167. (test 1 'let (let foo () 1))
  168. (test '((6 1 3) (-5 -2)) 'let
  169.       (let loop ((numbers '(3 -2 1 6 -5))
  170.          (nonneg '())
  171.          (neg '()))
  172.     (cond ((null? numbers) (list nonneg neg))
  173.           ((negative? (car numbers))
  174.            (loop (cdr numbers)
  175.              nonneg
  176.              (cons (car numbers) neg)))
  177.           (else
  178.            (loop (cdr numbers)
  179.              (cons (car numbers) nonneg)
  180.              neg)))))
  181. (SECTION 4 2 6)
  182. (test '(list 3 4) 'quasiquote `(list ,(+ 1 2) 4))
  183. (test '(list a (quote a)) 'quasiquote (let ((name 'a)) `(list ,name ',name)))
  184. (test '(a 3 4 5 6 b) 'quasiquote `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b))
  185. (test '((foo 7) . cons)
  186.     'quasiquote
  187.     `((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))))
  188.  
  189. ;;; sqt is defined here because not all implementations are required to
  190. ;;; support it. 
  191. (define (sqt x)
  192.     (do ((i 0 (+ i 1)))
  193.         ((> (* i i) x) (- i 1))))
  194.  
  195. (test '#(10 5 2 4 3 8) 'quasiquote `#(10 5 ,(sqt 4) ,@(map sqt '(16 9)) 8))
  196. (test 5 'quasiquote `,(+ 2 3))
  197. (test '(a `(b ,(+ 1 2) ,(foo 4 d) e) f)
  198.       'quasiquote `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f))
  199. (test '(a `(b ,x ,'y d) e) 'quasiquote
  200.     (let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e)))
  201. (test '(list 3 4) 'quasiquote (quasiquote (list (unquote (+ 1 2)) 4)))
  202. (test '`(list ,(+ 1 2) 4) 'quasiquote '(quasiquote (list (unquote (+ 1 2)) 4)))
  203. (SECTION 5 2 1)
  204. (define add3 (lambda (x) (+ x 3)))
  205. (test 6 'define (add3 3))
  206. (define first car)
  207. (test 1 'define (first '(1 2)))
  208. (SECTION 5 2 2)
  209. (test 45 'define
  210.     (let ((x 5))
  211.         (define foo (lambda (y) (bar x y)))
  212.         (define bar (lambda (a b) (+ (* a b) a)))
  213.         (foo (+ x 3))))
  214. (define x 34)
  215. (define (foo) (define x 5) x)
  216. (test 5 foo)
  217. (test 34 'define x)
  218. (define foo (lambda () (define x 5) x))
  219. (test 5 foo)
  220. (test 34 'define x)
  221. (define (foo x) ((lambda () (define x 5) x)) x)
  222. (test 88 foo 88)
  223. (test 4 foo 4)
  224. (test 34 'define x)
  225. (SECTION 6 1)
  226. (test #f not #t)
  227. (test #f not 3)
  228. (test #f not (list 3))
  229. (test #t not #f)
  230. (test #f not '())
  231. (test #f not (list))
  232. (test #f not 'nil)
  233.  
  234. (test #t boolean? #f)
  235. (test #f boolean? 0)
  236. (test #f boolean? '())
  237. (SECTION 6 2)
  238. (test #t eqv? 'a 'a)
  239. (test #f eqv? 'a 'b)
  240. (test #t eqv? 2 2)
  241. (test #t eqv? '() '())
  242. (test #t eqv? '10000 '10000)
  243. (test #f eqv? (cons 1 2)(cons 1 2))
  244. (test #f eqv? (lambda () 1) (lambda () 2))
  245. (test #f eqv? #f 'nil)
  246. (let ((p (lambda (x) x)))
  247.   (test #t eqv? p p))
  248. (define gen-counter
  249.  (lambda ()
  250.    (let ((n 0))
  251.       (lambda () (set! n (+ n 1)) n))))
  252. (let ((g (gen-counter))) (test #t eqv? g g))
  253. (test #f eqv? (gen-counter) (gen-counter))
  254. (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
  255.      (g (lambda () (if (eqv? f g) 'g 'both))))
  256.   (test #f eqv? f g))
  257.  
  258. (test #t eq? 'a 'a)
  259. (test #f eq? (list 'a) (list 'a))
  260. (test #t eq? '() '())
  261. (test #t eq? car car)
  262. (let ((x '(a))) (test #t eq? x x))
  263. (let ((x '#())) (test #t eq? x x))
  264. (let ((x (lambda (x) x))) (test #t eq? x x))
  265.  
  266. (test #t equal? 'a 'a)
  267. (test #t equal? '(a) '(a))
  268. (test #t equal? '(a (b) c) '(a (b) c))
  269. (test #t equal? "abc" "abc")
  270. (test #t equal? 2 2)
  271. (test #t equal? (make-vector 5 'a) (make-vector 5 'a))
  272. (SECTION 6 3)
  273. (test '(a b c d e) 'dot '(a . (b . (c . (d . (e . ()))))))
  274. (define x (list 'a 'b 'c))
  275. (define y x)
  276. (and list? (test #t list? y))
  277. (set-cdr! x 4)
  278. (test '(a . 4) 'set-cdr! x)
  279. (test #t eqv? x y)
  280. (test '(a b c . d) 'dot '(a . (b . (c . d))))
  281. (and list? (test #f list? y))
  282. (and list? (let ((x (list 'a))) (set-cdr! x x) (test #f 'list? (list? x))))
  283.  
  284. (test #t pair? '(a . b))
  285. (test #t pair? '(a . 1))
  286. (test #t pair? '(a b c))
  287. (test #f pair? '())
  288. (test #f pair? '#(a b))
  289.  
  290. (test '(a) cons 'a '())
  291. (test '((a) b c d) cons '(a) '(b c d))
  292. (test '("a" b c) cons "a" '(b c))
  293. (test '(a . 3) cons 'a 3)
  294. (test '((a b) . c) cons '(a b) 'c)
  295.  
  296. (test 'a car '(a b c))
  297. (test '(a) car '((a) b c d))
  298. (test 1 car '(1 . 2))
  299.  
  300. (test '(b c d) cdr '((a) b c d))
  301. (test 2 cdr '(1 . 2))
  302.  
  303. (test '(a 7 c) list 'a (+ 3 4) 'c)
  304. (test '() list)
  305.  
  306. (test 3 length '(a b c))
  307. (test 3 length '(a (b) (c d e)))
  308. (test 0 length '())
  309.  
  310. (test '(x y) append '(x) '(y))
  311. (test '(a b c d) append '(a) '(b c d))
  312. (test '(a (b) (c)) append '(a (b)) '((c)))
  313. (test '() append)
  314. (test '(a b c . d) append '(a b) '(c . d))
  315. (test 'a append '() 'a)
  316.  
  317. (test '(c b a) reverse '(a b c))
  318. (test '((e (f)) d (b c) a) reverse '(a (b c) d (e (f))))
  319.  
  320. (test 'c list-ref '(a b c d) 2)
  321.  
  322. (test '(a b c) memq 'a '(a b c))
  323. (test '(b c) memq 'b '(a b c))
  324. (test '#f memq 'a '(b c d))
  325. (test '#f memq (list 'a) '(b (a) c))
  326. (test '((a) c) member (list 'a) '(b (a) c))
  327. (test '(101 102) memv 101 '(100 101 102))
  328.  
  329. (define e '((a 1) (b 2) (c 3)))
  330. (test '(a 1) assq 'a e)
  331. (test '(b 2) assq 'b e)
  332. (test #f assq 'd e)
  333. (test #f assq (list 'a) '(((a)) ((b)) ((c))))
  334. (test '((a)) assoc (list 'a) '(((a)) ((b)) ((c))))
  335. (test '(5 7) assv 5 '((2 3) (5 7) (11 13)))
  336. (SECTION 6 4)
  337. (test #t symbol? 'foo)
  338. (test #t symbol? (car '(a b)))
  339. (test #f symbol? "bar")
  340. (test #t symbol? 'nil)
  341. (test #f symbol? '())
  342. (test #f symbol? #f)
  343. ;;; But first, what case are symbols in?  Determine the standard case:
  344. (define char-standard-case char-upcase)
  345. (if (string=? (symbol->string 'A) "a")
  346.     (set! char-standard-case char-downcase))
  347. (test #t 'standard-case
  348.       (string=? (symbol->string 'a) (symbol->string 'A)))
  349. (test #t 'standard-case
  350.       (or (string=? (symbol->string 'a) "A")
  351.       (string=? (symbol->string 'A) "a")))
  352. (define (str-copy s)
  353.   (let ((v (make-string (string-length s))))
  354.     (do ((i (- (string-length v) 1) (- i 1)))
  355.     ((< i 0) v)
  356.       (string-set! v i (string-ref s i)))))
  357. (define (string-standard-case s)
  358.   (set! s (str-copy s))
  359.   (do ((i 0 (+ 1 i))
  360.        (sl (string-length s)))
  361.       ((>= i sl) s)
  362.       (string-set! s i (char-standard-case (string-ref s i)))))
  363. (test (string-standard-case "flying-fish") symbol->string 'flying-fish)
  364. (test (string-standard-case "martin") symbol->string 'Martin)
  365. (test "Malvina" symbol->string (string->symbol "Malvina"))
  366. (test #t 'standard-case (eq? 'a 'A))
  367.  
  368. (define x (string #\a #\b))
  369. (define y (string->symbol x))
  370. (string-set! x 0 #\c)
  371. (test "cb" 'string-set! x)
  372. (test "ab" symbol->string y)
  373. (test y string->symbol "ab")
  374.  
  375. (test #t eq? 'mISSISSIppi 'mississippi)
  376. (test #f 'string->symbol (eq? 'bitBlt (string->symbol "bitBlt")))
  377. (test 'JollyWog string->symbol (symbol->string 'JollyWog))
  378.  
  379. (SECTION 6 5 5)
  380. (test #t number? 3)
  381. (test #t complex? 3)
  382. (test #t real? 3)
  383. (test #t rational? 3)
  384. (test #t integer? 3)
  385.  
  386. (test #t exact? 3)
  387. (test #f inexact? 3)
  388.  
  389. (test #t = 22 22 22)
  390. (test #t = 22 22)
  391. (test #f = 34 34 35)
  392. (test #f = 34 35)
  393. (test #t > 3 -6246)
  394. (test #f > 9 9 -2424)
  395. (test #t >= 3 -4 -6246)
  396. (test #t >= 9 9)
  397. (test #f >= 8 9)
  398. (test #t < -1 2 3 4 5 6 7 8)
  399. (test #f < -1 2 3 4 4 5 6 7)
  400. (test #t <= -1 2 3 4 5 6 7 8)
  401. (test #t <= -1 2 3 4 4 5 6 7)
  402. (test #f < 1 3 2)
  403. (test #f >= 1 3 2)
  404.  
  405. (test #t zero? 0)
  406. (test #f zero? 1)
  407. (test #f zero? -1)
  408. (test #f zero? -100)
  409. (test #t positive? 4)
  410. (test #f positive? -4)
  411. (test #f positive? 0)
  412. (test #f negative? 4)
  413. (test #t negative? -4)
  414. (test #f negative? 0)
  415. (test #t odd? 3)
  416. (test #f odd? 2)
  417. (test #f odd? -4)
  418. (test #t odd? -1)
  419. (test #f even? 3)
  420. (test #t even? 2)
  421. (test #t even? -4)
  422. (test #f even? -1)
  423.  
  424. (test 38 max 34 5 7 38 6)
  425. (test -24 min 3  5 5 330 4 -24)
  426.  
  427. (test 7 + 3 4)
  428. (test '3 + 3)
  429. (test 0 +)
  430. (test 4 * 4)
  431. (test 1 *)
  432.  
  433. (test -1 - 3 4)
  434. (test -3 - 3)
  435. (test 7 abs -7)
  436. (test 7 abs 7)
  437. (test 0 abs 0)
  438.  
  439. (test 5 quotient 35 7)
  440. (test -5 quotient -35 7)
  441. (test -5 quotient 35 -7)
  442. (test 5 quotient -35 -7)
  443. (test 1 modulo 13 4)
  444. (test 1 remainder 13 4)
  445. (test 3 modulo -13 4)
  446. (test -1 remainder -13 4)
  447. (test -3 modulo 13 -4)
  448. (test 1 remainder 13 -4)
  449. (test -1 modulo -13 -4)
  450. (test -1 remainder -13 -4)
  451. (define (divtest n1 n2)
  452.     (= n1 (+ (* n2 (quotient n1 n2))
  453.          (remainder n1 n2))))
  454. (test #t divtest 238 9)
  455. (test #t divtest -238 9)
  456. (test #t divtest 238 -9)
  457. (test #t divtest -238 -9)
  458.  
  459. (test 4 gcd 0 4)
  460. (test 4 gcd -4 0)
  461. (test 4 gcd 32 -36)
  462. (test 0 gcd)
  463. (test 288 lcm 32 -36)
  464. (test 1 lcm)
  465.  
  466. ;;;;From: fred@sce.carleton.ca (Fred J Kaudel)
  467. ;;; Modified by jaffer.
  468. (define (test-inexact)
  469.   (define f3.9 (string->number "3.9"))
  470.   (define f4.0 (string->number "4.0"))
  471.   (define f-3.25 (string->number "-3.25"))
  472.   (define f.25 (string->number ".25"))
  473.   (define wto write-test-obj)
  474.   (define dto display-test-obj)
  475.   (define lto load-test-obj)
  476.   (newline)
  477.   (display ";testing inexact numbers; ")
  478.   (newline)
  479.   (SECTION 6 5 5)
  480.   (test #t inexact? f3.9)
  481.   (test #t 'inexact? (inexact? (max f3.9 4)))
  482.   (test f4.0 'max (max f3.9 4))
  483.   (test f4.0 'exact->inexact (exact->inexact 4))
  484.   (set! write-test-obj (list f.25 f-3.25));.25 inexact errors less likely.
  485.   (set! display-test-obj (list f.25 f-3.25));.3 often has such errors (~10^-13)
  486.   (set! load-test-obj (list 'define 'foo (list 'quote write-test-obj)))
  487.   (test #t call-with-output-file
  488.       "tmp3"
  489.       (lambda (test-file)
  490.     (write-char #\; test-file)
  491.     (display write-test-obj test-file)
  492.     (newline test-file)
  493.     (write load-test-obj test-file)
  494.     (output-port? test-file)))
  495.   (check-test-file "tmp3")
  496.   (set! write-test-obj wto)
  497.   (set! display-test-obj dto)
  498.   (set! load-test-obj lto)
  499.   (report-errs))
  500.  
  501. (define (test-bignum)
  502.   (define tb
  503.     (lambda (n1 n2)
  504.       (= n1 (+ (* n2 (quotient n1 n2))
  505.            (remainder n1 n2)))))
  506.   (newline)
  507.   (display ";testing bignums; ")
  508.   (newline)
  509.   (section 6 5 5)
  510.   (test #t 'remainder (tb 281474976710655 65535))
  511.   (test #t 'remainder (tb 281474976710654 65535))
  512.   (SECTION 6 5 6)
  513.   (test 281474976710655 string->number "281474976710655")
  514.   (test "281474976710655" number->string 281474976710655)
  515.   (report-errs))
  516.  
  517. (SECTION 6 5 6)
  518. (test "0" number->string 0)
  519. (test "100" number->string 100)
  520. (test "100" number->string 256 16)
  521. (test 100 string->number "100")
  522. (test 256 string->number "100" 16)
  523. (test #f string->number "")
  524. (test #f string->number ".")
  525. (test #f string->number "d")
  526. (test #f string->number "D")
  527. (test #f string->number "i")
  528. (test #f string->number "I")
  529. (test #f string->number "3i")
  530. (test #f string->number "3I")
  531. (test #f string->number "33i")
  532. (test #f string->number "33I")
  533. (test #f string->number "3.3i")
  534. (test #f string->number "3.3I")
  535. (test #f string->number "-")
  536. (test #f string->number "+")
  537.  
  538. (SECTION 6 6)
  539. (test #t eqv? '#\  #\Space)
  540. (test #t eqv? #\space '#\Space)
  541. (test #t char? #\a)
  542. (test #t char? #\()
  543. (test #t char? #\ )
  544. (test #t char? '#\newline)
  545.  
  546. (test #f char=? #\A #\B)
  547. (test #f char=? #\a #\b)
  548. (test #f char=? #\9 #\0)
  549. (test #t char=? #\A #\A)
  550.  
  551. (test #t char<? #\A #\B)
  552. (test #t char<? #\a #\b)
  553. (test #f char<? #\9 #\0)
  554. (test #f char<? #\A #\A)
  555.  
  556. (test #f char>? #\A #\B)
  557. (test #f char>? #\a #\b)
  558. (test #t char>? #\9 #\0)
  559. (test #f char>? #\A #\A)
  560.  
  561. (test #t char<=? #\A #\B)
  562. (test #t char<=? #\a #\b)
  563. (test #f char<=? #\9 #\0)
  564. (test #t char<=? #\A #\A)
  565.  
  566. (test #f char>=? #\A #\B)
  567. (test #f char>=? #\a #\b)
  568. (test #t char>=? #\9 #\0)
  569. (test #t char>=? #\A #\A)
  570.  
  571. (test #f char-ci=? #\A #\B)
  572. (test #f char-ci=? #\a #\B)
  573. (test #f char-ci=? #\A #\b)
  574. (test #f char-ci=? #\a #\b)
  575. (test #f char-ci=? #\9 #\0)
  576. (test #t char-ci=? #\A #\A)
  577. (test #t char-ci=? #\A #\a)
  578.  
  579. (test #t char-ci<? #\A #\B)
  580. (test #t char-ci<? #\a #\B)
  581. (test #t char-ci<? #\A #\b)
  582. (test #t char-ci<? #\a #\b)
  583. (test #f char-ci<? #\9 #\0)
  584. (test #f char-ci<? #\A #\A)
  585. (test #f char-ci<? #\A #\a)
  586.  
  587. (test #f char-ci>? #\A #\B)
  588. (test #f char-ci>? #\a #\B)
  589. (test #f char-ci>? #\A #\b)
  590. (test #f char-ci>? #\a #\b)
  591. (test #t char-ci>? #\9 #\0)
  592. (test #f char-ci>? #\A #\A)
  593. (test #f char-ci>? #\A #\a)
  594.  
  595. (test #t char-ci<=? #\A #\B)
  596. (test #t char-ci<=? #\a #\B)
  597. (test #t char-ci<=? #\A #\b)
  598. (test #t char-ci<=? #\a #\b)
  599. (test #f char-ci<=? #\9 #\0)
  600. (test #t char-ci<=? #\A #\A)
  601. (test #t char-ci<=? #\A #\a)
  602.  
  603. (test #f char-ci>=? #\A #\B)
  604. (test #f char-ci>=? #\a #\B)
  605. (test #f char-ci>=? #\A #\b)
  606. (test #f char-ci>=? #\a #\b)
  607. (test #t char-ci>=? #\9 #\0)
  608. (test #t char-ci>=? #\A #\A)
  609. (test #t char-ci>=? #\A #\a)
  610.  
  611. (test #t char-alphabetic? #\a)
  612. (test #t char-alphabetic? #\A)
  613. (test #t char-alphabetic? #\z)
  614. (test #t char-alphabetic? #\Z)
  615. (test #f char-alphabetic? #\0)
  616. (test #f char-alphabetic? #\9)
  617. (test #f char-alphabetic? #\space)
  618. (test #f char-alphabetic? #\;)
  619.  
  620. (test #f char-numeric? #\a)
  621. (test #f char-numeric? #\A)
  622. (test #f char-numeric? #\z)
  623. (test #f char-numeric? #\Z)
  624. (test #t char-numeric? #\0)
  625. (test #t char-numeric? #\9)
  626. (test #f char-numeric? #\space)
  627. (test #f char-numeric? #\;)
  628.  
  629. (test #f char-whitespace? #\a)
  630. (test #f char-whitespace? #\A)
  631. (test #f char-whitespace? #\z)
  632. (test #f char-whitespace? #\Z)
  633. (test #f char-whitespace? #\0)
  634. (test #f char-whitespace? #\9)
  635. (test #t char-whitespace? #\space)
  636. (test #f char-whitespace? #\;)
  637.  
  638. (test #f char-upper-case? #\0)
  639. (test #f char-upper-case? #\9)
  640. (test #f char-upper-case? #\space)
  641. (test #f char-upper-case? #\;)
  642.  
  643. (test #f char-lower-case? #\0)
  644. (test #f char-lower-case? #\9)
  645. (test #f char-lower-case? #\space)
  646. (test #f char-lower-case? #\;)
  647.  
  648. (test #\. integer->char (char->integer #\.))
  649. (test #\A integer->char (char->integer #\A))
  650. (test #\a integer->char (char->integer #\a))
  651. (test #\A char-upcase #\A)
  652. (test #\A char-upcase #\a)
  653. (test #\a char-downcase #\A)
  654. (test #\a char-downcase #\a)
  655. (SECTION 6 7)
  656. (test #t string? "The word \"recursion\\\" has many meanings.")
  657. (test #t string? "")
  658. (define f (make-string 3 #\*))
  659. (test "?**" 'string-set! (begin (string-set! f 0 #\?) f))
  660. (test "abc" string #\a #\b #\c)
  661. (test "" string)
  662. (test 3 string-length "abc")
  663. (test #\a string-ref "abc" 0)
  664. (test #\c string-ref "abc" 2)
  665. (test 0 string-length "")
  666. (test "" substring "ab" 0 0)
  667. (test "" substring "ab" 1 1)
  668. (test "" substring "ab" 2 2)
  669. (test "a" substring "ab" 0 1)
  670. (test "b" substring "ab" 1 2)
  671. (test "ab" substring "ab" 0 2)
  672. (test "foobar" string-append "foo" "bar")
  673. (test "foo" string-append "foo")
  674. (test "foo" string-append "foo" "")
  675. (test "foo" string-append "" "foo")
  676. (test "" string-append)
  677. (test "" make-string 0)
  678. (test #t string=? "" "")
  679. (test #f string<? "" "")
  680. (test #f string>? "" "")
  681. (test #t string<=? "" "")
  682. (test #t string>=? "" "")
  683. (test #t string-ci=? "" "")
  684. (test #f string-ci<? "" "")
  685. (test #f string-ci>? "" "")
  686. (test #t string-ci<=? "" "")
  687. (test #t string-ci>=? "" "")
  688.  
  689. (test #f string=? "A" "B")
  690. (test #f string=? "a" "b")
  691. (test #f string=? "9" "0")
  692. (test #t string=? "A" "A")
  693.  
  694. (test #t string<? "A" "B")
  695. (test #t string<? "a" "b")
  696. (test #f string<? "9" "0")
  697. (test #f string<? "A" "A")
  698.  
  699. (test #f string>? "A" "B")
  700. (test #f string>? "a" "b")
  701. (test #t string>? "9" "0")
  702. (test #f string>? "A" "A")
  703.  
  704. (test #t string<=? "A" "B")
  705. (test #t string<=? "a" "b")
  706. (test #f string<=? "9" "0")
  707. (test #t string<=? "A" "A")
  708.  
  709. (test #f string>=? "A" "B")
  710. (test #f string>=? "a" "b")
  711. (test #t string>=? "9" "0")
  712. (test #t string>=? "A" "A")
  713.  
  714. (test #f string-ci=? "A" "B")
  715. (test #f string-ci=? "a" "B")
  716. (test #f string-ci=? "A" "b")
  717. (test #f string-ci=? "a" "b")
  718. (test #f string-ci=? "9" "0")
  719. (test #t string-ci=? "A" "A")
  720. (test #t string-ci=? "A" "a")
  721.  
  722. (test #t string-ci<? "A" "B")
  723. (test #t string-ci<? "a" "B")
  724. (test #t string-ci<? "A" "b")
  725. (test #t string-ci<? "a" "b")
  726. (test #f string-ci<? "9" "0")
  727. (test #f string-ci<? "A" "A")
  728. (test #f string-ci<? "A" "a")
  729.  
  730. (test #f string-ci>? "A" "B")
  731. (test #f string-ci>? "a" "B")
  732. (test #f string-ci>? "A" "b")
  733. (test #f string-ci>? "a" "b")
  734. (test #t string-ci>? "9" "0")
  735. (test #f string-ci>? "A" "A")
  736. (test #f string-ci>? "A" "a")
  737.  
  738. (test #t string-ci<=? "A" "B")
  739. (test #t string-ci<=? "a" "B")
  740. (test #t string-ci<=? "A" "b")
  741. (test #t string-ci<=? "a" "b")
  742. (test #f string-ci<=? "9" "0")
  743. (test #t string-ci<=? "A" "A")
  744. (test #t string-ci<=? "A" "a")
  745.  
  746. (test #f string-ci>=? "A" "B")
  747. (test #f string-ci>=? "a" "B")
  748. (test #f string-ci>=? "A" "b")
  749. (test #f string-ci>=? "a" "b")
  750. (test #t string-ci>=? "9" "0")
  751. (test #t string-ci>=? "A" "A")
  752. (test #t string-ci>=? "A" "a")
  753. (SECTION 6 8)
  754. (test #t vector? '#(0 (2 2 2 2) "Anna"))
  755. (test #t vector? '#())
  756. (test '#(a b c) vector 'a 'b 'c)
  757. (test '#() vector)
  758. (test 3 vector-length '#(0 (2 2 2 2) "Anna"))
  759. (test 0 vector-length '#())
  760. (test 8 vector-ref '#(1 1 2 3 5 8 13 21) 5)
  761. (test '#(0 ("Sue" "Sue") "Anna") 'vector-set
  762.     (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  763.       (vector-set! vec 1 '("Sue" "Sue"))
  764.       vec))
  765. (test '#(hi hi) make-vector 2 'hi)
  766. (test '#() make-vector 0)
  767. (test '#() make-vector 0 'a)
  768. (SECTION 6 9)
  769. (test #t procedure? car)
  770. (test #f procedure? 'car)
  771. (test #t procedure? (lambda (x) (* x x)))
  772. (test #f procedure? '(lambda (x) (* x x)))
  773. (test #t call-with-current-continuation procedure?)
  774. (test 7 apply + (list 3 4))
  775. (test 7 apply (lambda (a b) (+ a b)) (list 3 4))
  776. (test 17 apply + 10 (list 3 4))
  777. (test '() apply list '())
  778. (define compose (lambda (f g) (lambda args (f (apply g args)))))
  779. (test 30 (compose sqt *) 12 75)
  780.  
  781. (test '(b e h) map cadr '((a b) (d e) (g h)))
  782. (test '(5 7 9) map + '(1 2 3) '(4 5 6))
  783. (test '#(0 1 4 9 16) 'for-each
  784.     (let ((v (make-vector 5)))
  785.         (for-each (lambda (i) (vector-set! v i (* i i)))
  786.             '(0 1 2 3 4))
  787.         v))
  788. (test -3 call-with-current-continuation
  789.         (lambda (exit)
  790.          (for-each (lambda (x) (if (negative? x) (exit x)))
  791.              '(54 0 37 -3 245 19))
  792.         #t))
  793. (define list-length
  794.  (lambda (obj)
  795.   (call-with-current-continuation
  796.    (lambda (return)
  797.     (letrec ((r (lambda (obj) (cond ((null? obj) 0)
  798.                 ((pair? obj) (+ (r (cdr obj)) 1))
  799.                 (else (return #f))))))
  800.     (r obj))))))
  801. (test 4 list-length '(1 2 3 4))
  802. (test #f list-length '(a b . c))
  803. (test '() map cadr '())
  804.  
  805. ;;; This tests full conformance of call-with-current-continuation.  It
  806. ;;; is a separate test because some schemes do not support call/cc
  807. ;;; other than escape procedures.  I am indebted to
  808. ;;; raja@copper.ucs.indiana.edu (Raja Sooriamurthi) for fixing this
  809. ;;; code.  The function leaf-eq? compares the leaves of 2 arbitrary
  810. ;;; trees constructed of conses.  
  811. (define (next-leaf-generator obj eot)
  812.   (letrec ((return #f)
  813.        (cont (lambda (x)
  814.            (recur obj)
  815.            (set! cont (lambda (x) (return eot)))
  816.            (cont #f)))
  817.        (recur (lambda (obj)
  818.               (if (pair? obj)
  819.               (for-each recur obj)
  820.               (call-with-current-continuation
  821.                (lambda (c)
  822.                  (set! cont c)
  823.                  (return obj)))))))
  824.     (lambda () (call-with-current-continuation
  825.         (lambda (ret) (set! return ret) (cont #f))))))
  826. (define (leaf-eq? x y)
  827.   (let* ((eot (list 'eot))
  828.      (xf (next-leaf-generator x eot))
  829.      (yf (next-leaf-generator y eot)))
  830.     (letrec ((loop (lambda (x y)
  831.              (cond ((not (eq? x y)) #f)
  832.                ((eq? eot x) #t)
  833.                (else (loop (xf) (yf)))))))
  834.       (loop (xf) (yf)))))
  835. (define (test-cont)
  836.   (newline)
  837.   (display ";testing continuations; ")
  838.   (newline)
  839.   (SECTION 6 9)
  840.   (test #t leaf-eq? '(a (b (c))) '((a) b c))
  841.   (test #f leaf-eq? '(a (b (c))) '((a) b c d))
  842.   (report-errs))
  843.  
  844. ;;; Test Optional R4RS DELAY syntax and FORCE procedure
  845. (define (test-delay)
  846.   (newline)
  847.   (display ";testing DELAY and FORCE; ")
  848.   (newline)
  849.   (SECTION 6 9)
  850.   (test 3 'delay (force (delay (+ 1 2))))
  851.   (test '(3 3) 'delay (let ((p (delay (+ 1 2))))
  852.             (list (force p) (force p))))
  853.   (test 2 'delay (letrec ((a-stream
  854.                (letrec ((next (lambda (n)
  855.                         (cons n (delay (next (+ n 1)))))))
  856.                  (next 0)))
  857.               (head car)
  858.               (tail (lambda (stream) (force (cdr stream)))))
  859.            (head (tail (tail a-stream)))))
  860.   (letrec ((count 0)
  861.        (p (delay (begin (set! count (+ count 1))
  862.                 (if (> count x)
  863.                 count
  864.                 (force p)))))
  865.        (x 5))
  866.     (test 6 force p)
  867.     (set! x 10)
  868.     (test 6 force p))
  869.   (test 3 'force
  870.     (letrec ((p (delay (if c 3 (begin (set! c #t) (+ (force p) 1)))))
  871.          (c #f))
  872.       (force p)))
  873.   (report-errs))
  874.  
  875. (SECTION 6 10 1)
  876. (test #t input-port? (current-input-port))
  877. (test #t output-port? (current-output-port))
  878. (test #t call-with-input-file "test.scm" input-port?)
  879. (define this-file (open-input-file "test.scm"))
  880. (test #t input-port? this-file)
  881. (SECTION 6 10 2)
  882. (test #\; peek-char this-file)
  883. (test #\; read-char this-file)
  884. (test '(define cur-section '()) read this-file)
  885. (test #\( peek-char this-file)
  886. (test '(define errs '()) read this-file)
  887. (close-input-port this-file)
  888. (close-input-port this-file)
  889. (define (check-test-file name)
  890.   (define test-file (open-input-file name))
  891.   (test #t 'input-port?
  892.     (call-with-input-file
  893.         name
  894.       (lambda (test-file)
  895.         (test load-test-obj read test-file)
  896.         (test #t eof-object? (peek-char test-file))
  897.         (test #t eof-object? (read-char test-file))
  898.         (input-port? test-file))))
  899.   (test #\; read-char test-file)
  900.   (test display-test-obj read test-file)
  901.   (test load-test-obj read test-file)
  902.   (close-input-port test-file))
  903. (SECTION 6 10 3)
  904. (define write-test-obj
  905.   '(#t #f #\a () 9739 -3 . #((test) "te \" \" st" "" test #() b c)))
  906. (define display-test-obj
  907.   '(#t #f a () 9739 -3 . #((test) te " " st test #() b c)))
  908. (define load-test-obj
  909.   (list 'define 'foo (list 'quote write-test-obj)))
  910. (test #t call-with-output-file
  911.       "tmp1"
  912.       (lambda (test-file)
  913.     (write-char #\; test-file)
  914.     (display write-test-obj test-file)
  915.     (newline test-file)
  916.     (write load-test-obj test-file)
  917.     (output-port? test-file)))
  918. (check-test-file "tmp1")
  919.  
  920. (define test-file (open-output-file "tmp2"))
  921. (write-char #\; test-file)
  922. (display write-test-obj test-file)
  923. (newline test-file)
  924. (write load-test-obj test-file)
  925. (test #t output-port? test-file)
  926. (close-output-port test-file)
  927. (check-test-file "tmp2")
  928. (define (test-sc4)
  929.   (newline)
  930.   (display ";testing scheme 4 functions; ")
  931.   (newline)
  932.   (SECTION 6 7)
  933.   (test '(#\P #\space #\l) string->list "P l")
  934.   (test '() string->list "")
  935.   (test "1\\\"" list->string '(#\1 #\\ #\"))
  936.   (test "" list->string '())
  937.   (SECTION 6 8)
  938.   (test '(dah dah didah) vector->list '#(dah dah didah))
  939.   (test '() vector->list '#())
  940.   (test '#(dididit dah) list->vector '(dididit dah))
  941.   (test '#() list->vector '())
  942.   (SECTION 6 10 4)
  943.   (load "tmp1")
  944.   (test write-test-obj 'load foo)
  945.   (report-errs))
  946.  
  947. (report-errs)
  948. (if (and (string->number "0.0") (inexact? (string->number "0.0")))
  949.     (test-inexact))
  950.  
  951. (let ((n (string->number "281474976710655")))
  952.   (if (and n (exact? n))
  953.       (test-bignum)))
  954.  
  955. (newline)
  956. (display "To fully test continuations, Scheme 4, and DELAY/FORCE do:")
  957. (newline)
  958. (display "(test-cont) (test-sc4) (test-delay)")
  959. (newline)
  960. "last item in file"
  961.